package com.yiya.ms.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Attribute;
import com.yiya.model.AttributeModel;
import com.yiya.service.AttributeService;
import com.yiya.utils.HtmlUtil;

@Controller
@RequestMapping("/attribute")
public class AttributeAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(AttributeAction.class);
	
	@Autowired
	private AttributeService attributeService;
	
	@RequestMapping("/list") 
	public ModelAndView list(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("attribute/attribute",context); 
	}
	
	@RequestMapping("/dataList") 
	public void  datalist(AttributeModel model,HttpServletResponse response) throws Exception{
		List<Attribute> attributes = attributeService.queryByList(model);
		int count = attributeService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", attributes);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
		
	}
	
	@RequestMapping("/save")
	public void save(Attribute bean,HttpServletRequest request,HttpServletResponse response){
		if(bean.getId() == null){
			attributeService.add(bean);
		}else{
			attributeService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		Attribute attribute = attributeService.queryById(id);
		if(attribute  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", attribute);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		attributeService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}
	
	@RequestMapping("/loadAttrList")
	public void loadAttrList(AttributeModel model,HttpServletResponse response) throws Exception{
		List<Attribute>  attributes = attributeService.queryAllList(model);
		HtmlUtil.writerJson(response, attributes);
	}
}
